package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ImageView;

import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;

import com.oscar.ehome.bean.pagebean.InsuranceBean;
import com.oscar.ehome.bean.pagebean.WorkLogBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshBase;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshListView;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.view.loginview.LoginActivity;

import com.oscar.ehome.view.pageview.adapter.InsuranceAdapter;
import com.oscar.ehome.view.pageview.view.InuranceListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/8/31
 * 保险列表页
 */

public class InsuranceActivity extends BasesActivity<InuranceListView, PagePresenter> implements PullToRefreshBase.OnRefreshListener,
        AdapterView.OnItemClickListener, ProgressFrameLayout.AgainRequestListener, InuranceListView {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 添加
     */
    @BindView(R.id.title_img_right)
    ImageView titleImgRight;
    /**
     * 添加
     */
    @BindView(R.id.title_right_layput)
    RelativeLayout rightLayout;
    /**
     * 主题色背景
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     *
     */
    @BindView(R.id.insura_fragment_listview)
    PullToRefreshListView insuraFragmentListview;
    /**
     *
     */
    @BindView(R.id.insura_fragment)
    ProgressFrameLayout insuraFragment;

    private List<InsuranceBean.ListInuranceBean> list = new ArrayList<>();
    InsuranceAdapter adapter;
    PagePresenter presenter;
    String token = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insurance);
        ButterKnife.bind(this);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(InsuranceActivity.this).getSystemConfig().getColor()));
        initView();
        onClick();
    }

    /**
     * 初始化
     */
    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        rightLayout.setVisibility(View.VISIBLE);
        titleImgRight.setImageResource(R.mipmap.jia);
        titleTopbar.setText("保险档案");

        token = SystemConfigFactory.getInstance(InsuranceActivity.this).getSystemConfig().getToken();
        presenter = new PagePresenter(this);

        adapter = new InsuranceAdapter(InsuranceActivity.this, list);
        insuraFragmentListview.setAdapter(adapter);
    }


    /**
     * 加载数据
     */
    public void initData() {
        if (isNetWork()) {
            insuraFragment.stopRefresh();
            insuraFragmentListview.setVisibility(View.VISIBLE);
            presenter.getbaoxianList(token);
            showLoading();
        } else {
            insuraFragment.setVisibility(View.VISIBLE);
            insuraFragment.setLoadFail();
            insuraFragmentListview.setVisibility(View.GONE);
        }
    }

    public void onClick() {
        insuraFragmentListview.setPullLoadEnabled(true);
        insuraFragmentListview.setOnRefreshListener(this);
        insuraFragmentListview.setOnItemClickListener(this);
        insuraFragment.setAgainRequestListener(this);
    }

    @OnClick({R.id.title_layout_left, R.id.title_right_layput})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_layout_left://返回
                finish();
                break;
            case R.id.title_right_layput://添加
                Intent intent = new Intent(this, InsurawriteActivity.class);
                intent.putExtra("add", "1");
                intent.putExtra("id", "");
                startActivity(intent);
                break;
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (list.size() > 0) {
            if (list.get(position).getId() != null) {
                Intent intent = new Intent(InsuranceActivity.this, InsuranceMessageActivity.class);
                intent.putExtra("id", list.get(position).getId());
                startActivity(intent);
            }
        }
    }

    @Override
    public void getInsuranceList(InsuranceBean result) {
        dismissLoading();
        insuraFragmentListview.onPullDownRefreshComplete();
        insuraFragmentListview.onPullUpRefreshComplete();
        if (result != null&&result.getCount()>0) {
            list = new ArrayList<>();
            list = result.getList();
            adapter.setLists(list);
        }else {
            insuraFragmentListview.setVisibility(View.GONE);
            insuraFragment.setNoResult();
        }
    }

    @Override
    public void getWorkList(WorkLogBean result) {

    }

    @Override
    public void erroy() {
        dismissLoading();
        insuraFragmentListview.onPullDownRefreshComplete();
        insuraFragmentListview.onPullUpRefreshComplete();
        ToastUtil.show(this, getString(R.string.erry));
        insuraFragment.setLoadFail();
        insuraFragmentListview.setVisibility(View.GONE);
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        insuraFragmentListview.onPullDownRefreshComplete();
        insuraFragmentListview.onPullUpRefreshComplete();
        ToastUtil.show(this, result);

        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            insuraFragment.setLoadFail();
            insuraFragmentListview.setVisibility(View.GONE);
            insuraFragment.setNoResult();
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {

    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected PagePresenter createPresenter() {
        presenter = new PagePresenter(this);
        return presenter;
    }

    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }

    @Override
    public void againRequest() {
        if (isNetWork()) {
            initData();
        } else {
            ToastUtil.show(this, R.string.pull_to_refresh_network_error);
        }
    }

}
